package com.fm.openinstall.a;

import android.content.*;
import com.fm.openinstall.b.*;
import java.util.*;
import com.fm.openinstall.f.*;
import android.os.*;
import android.text.*;
import org.json.*;
import android.net.*;
import java.util.concurrent.*;

public class f
{
    CountDownLatch a;
    LinkedBlockingQueue b;
    Object c;
    private ThreadPoolExecutor d;
    private ThreadPoolExecutor e;
    private c f;
    private b g;
    private volatile com.fm.openinstall.b.c h;
    private com.fm.openinstall.e.f i;
    private String j;
    private Context k;
    private a l;
    private Map m;
    private Map n;

    private f() {
        this.a = new CountDownLatch(1);
        this.b = new LinkedBlockingQueue(1);
        this.c = new Object();
        this.f = com.fm.openinstall.f.c.a(f.class);
        this.g = null;
        this.h = com.fm.openinstall.b.c.a;
        this.d = new ThreadPoolExecutor(3, 10, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(5), new g(this), new i(this));
        this.e = new ThreadPoolExecutor(3, 10, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(5), new j(this), new k(this));
    }

    private long a(final int n) {
        if (n < 3) {
            return 1000L;
        }
        if (n < 8) {
            return 3000L;
        }
        return 5000L;
    }

    public static f a() {
        return r.a;
    }

    private a a(final a a) {
        final a l = this.l;
        if (l != null) {
            return l;
        }
        final a c = a.c(com.fm.openinstall.f.d.a(this.k, "FM_pb_data"));
        if (c != null) {
            return c;
        }
        com.fm.openinstall.f.d.a(this.k, "FM_pb_data", a.a(a));
        return a;
    }

    private com.fm.openinstall.c.b a(final long n) {
        if (this.h == com.fm.openinstall.b.c.c) {
            com.fm.openinstall.a.e.a(this.j, this.g.h());
        }
        Label_0061: {
            if (this.h != com.fm.openinstall.b.c.b) {
                break Label_0061;
            }
            this.b.offer(this.c);
            try {
                this.a.await(n, TimeUnit.SECONDS);
                String s;
                com.fm.openinstall.c.b b;
                StringBuilder sb;
                String s2;
                if (this.h == com.fm.openinstall.b.c.e) {
                    s = com.fm.openinstall.f.d.a(this.k, "FM_init_msg");
                    b = new com.fm.openinstall.c.b(com.fm.openinstall.c.c.c, -12);
                    sb = new StringBuilder();
                    s2 = "\u521d\u59cb\u5316\u65f6\u9519\u8bef\uff1a";
                }
                else {
                    if (this.h != com.fm.openinstall.b.c.d) {
                        return null;
                    }
                    s = com.fm.openinstall.f.d.a(this.k, "FM_init_msg");
                    b = new com.fm.openinstall.c.b(com.fm.openinstall.c.c.b, -11);
                    sb = new StringBuilder();
                    s2 = "\u521d\u59cb\u5316\u65f6\u5931\u8d25\uff1a";
                }
                sb.append(s2);
                sb.append(s);
                b.b(sb.toString());
                return b;
            }
            catch (InterruptedException ex) {
                return null;
            }
        }
    }

    private String a(final String s) {
        return String.format("https://%s/api/v2/android/%s/%s", "openinstall.io", this.j, s);
    }

    private Map a(final Context context) {
        if (this.m == null) {
            (this.m = com.fm.openinstall.a.d.a(context)).put("pkg", context.getPackageName());
            this.m.put("certFinger", com.fm.openinstall.a.a.b(context));
            this.m.put("version", Build.VERSION.RELEASE);
            this.m.put("versionCode", com.fm.openinstall.a.a.c(context));
            this.m.put("apiVersion", "2.2.2");
        }
        final b g = this.g;
        String s;
        if (g == null) {
            s = com.fm.openinstall.a.e.a(this.j);
        }
        else {
            s = g.h();
        }
        this.m.put("installId", s);
        return this.m;
    }

    private void a(final Map map, final a a) {
        if (a != null) {
            if (a.c(2)) {
                map.put("pbHtml", a.b());
            }
            if (a.c(1)) {
                map.put("pbText", a.a());
            }
        }
    }

    private com.fm.openinstall.c.a b() {
        return new com.fm.openinstall.c.a();
    }

    private Map b(final Context context) {
        if (this.n == null) {
            (this.n = com.fm.openinstall.a.d.a()).put("simulator", String.valueOf(v.a()));
            this.n.put("apkInfo", com.fm.openinstall.a.a.a(context));
            this.a(this.n, this.l);
        }
        final Map n = this.n;
        if (n != null && n.get("apkInfo") == null) {
            this.n.put("apkInfo", com.fm.openinstall.a.a.a(context));
        }
        return this.n;
    }

    private void b(final String s) {
        if (this.g == null) {
            this.g = new b();
        }
        if (TextUtils.isEmpty((CharSequence)s)) {
            return;
        }
        while (true) {
            while (true) {
                Label_0345: {
                    try {
                        final b a = com.fm.openinstall.b.b.a(s);
                        int n2;
                        final int n = n2 = 0;
                        if (a.g() != null) {
                            n2 = n;
                            if (!a.g().equals(this.g.g())) {
                                this.g.a(a.g());
                                n2 = 1;
                            }
                        }
                        int n3 = n2;
                        if (a.a() != null) {
                            n3 = n2;
                            if (!a.a().equals(this.g.a())) {
                                this.g.a(a.a());
                                n3 = 1;
                            }
                        }
                        int n4 = n3;
                        if (a.d() != null) {
                            n4 = n3;
                            if (!a.d().equals(this.g.d())) {
                                this.g.c(a.d());
                                if (this.g.e()) {
                                    this.i.a(a.g());
                                    break Label_0345;
                                }
                                this.i.b();
                                break Label_0345;
                            }
                        }
                        int n5 = n4;
                        if (a.c() != null) {
                            n5 = n4;
                            if (!a.c().equals(this.g.c())) {
                                this.g.b(a.c());
                                n5 = 1;
                            }
                        }
                        int n6 = n5;
                        if (!TextUtils.isEmpty((CharSequence)a.h())) {
                            n6 = n5;
                            if (!a.h().equals(this.g.h())) {
                                this.g.b(a.h());
                                com.fm.openinstall.a.e.b(this.j, a.h());
                                n6 = 1;
                            }
                        }
                        if (n6 != 0) {
                            com.fm.openinstall.f.d.a(this.k, "FM_config_data", this.g.i());
                        }
                        if (this.g != null && !TextUtils.isEmpty((CharSequence)this.g.h())) {
                            com.fm.openinstall.a.e.a(this.j, this.g.h());
                        }
                        return;
                    }
                    catch (JSONException ex) {
                        return;
                    }
                }
                int n4 = 1;
                continue;
            }
        }
    }

    public void a(final Uri uri, final a a, final q q) {
        final m m = new m(this, this.e, q, 10000L, uri, a);
        m.a("wakeUpRunnable");
        this.d.execute(m);
    }

    public void a(final q q) {
        final p p = new p(this, this.e, q, 30000L);
        p.a("statsRunnable$Register");
        this.d.execute(p);
    }

    public void a(final q q, final long n) {
        long n2 = n;
        if (n < 1000L) {
            n2 = 15000L;
        }
        final n n3 = new n(this, this.e, q, n2);
        n3.a("installRunnable");
        this.d.execute(n3);
    }

    public void a(final String j, final Context k) {
        // monitorenter(this)
        Label_0179: {
            if (j == null || k == null) {
                break Label_0179;
            }
            try {
                this.j = j;
                this.k = k;
                (this.i = com.fm.openinstall.e.f.a(k)).a();
                com.fm.openinstall.b.c h;
                if ((h = this.h) == com.fm.openinstall.b.c.a) {
                    h = com.fm.openinstall.b.c.a(com.fm.openinstall.f.d.b(k, "FM_init_state", com.fm.openinstall.b.c.a.a()));
                }
                if (h != com.fm.openinstall.b.c.a && h != com.fm.openinstall.b.c.d && h != com.fm.openinstall.b.c.e) {
                    this.b(com.fm.openinstall.f.d.a(this.k, "FM_config_data"));
                    this.h = h;
                }
                else {
                    this.h = com.fm.openinstall.b.c.b;
                    final a b = com.fm.openinstall.a.b.a(this.k).b();
                    com.fm.openinstall.a.b.a(this.k).c();
                    this.l = this.a(b);
                    final Thread thread = new Thread(new l(this));
                    thread.setName("thread-init");
                    thread.start();
                }
                return;
                throw new IllegalArgumentException("appKey\u548ccontext\u4e0d\u80fd\u4e3a\u7a7a");
            }
            finally {
            }
            // monitorexit(this)
        }
    }

    public void a(final String s, final q q) {
        final o o = new o(this, this.e, q, 30000L, s);
        o.a("statsRunnable$Wakeup");
        this.d.execute(o);
    }

    public void b(final String s, final q q) {
        final h h = new h(this, this.e, q, 30000L, s);
        h.a("statsRunnable$Event");
        h.run();
    }
}
